package org.shareworth.online_life.client.util;

import org.eclipse.jdt.internal.compiler.ast.ThisReference;

import com.extjs.gxt.ui.client.data.HttpProxy;
import com.extjs.gxt.ui.client.data.JsonLoadResultReader;
import com.extjs.gxt.ui.client.data.ListLoadResult;
import com.extjs.gxt.ui.client.data.ModelData;
import com.google.gwt.core.client.GWT;
import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestBuilder;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.RequestException;
import com.google.gwt.http.client.Response;
import com.google.gwt.http.client.URL;
import com.google.gwt.http.client.RequestBuilder.Method;

public class JSONRequest {

	private String url;
	private String method;
	private String requestData;
	private RequestCallback requestCallback;
	
	public JSONRequest(String url, String method, String requestData,
			RequestCallback requestCallback) {
		super();
		this.url = url;
		this.method = method;
		this.requestData = requestData;
		this.requestCallback = requestCallback;
	}

	public JSONRequest(String url, String method, String requestData) {
		super();
		this.url = url;
		this.method = method;
		this.requestData = requestData;
	}

	public JSONRequest(String url, String requestData,
			RequestCallback requestCallback) {
		super();
		this.url = url;
		this.requestData = requestData;
		this.requestCallback = requestCallback;
	}

	public JSONRequest(String url, RequestCallback requestCallback) {
		super();
		this.url = url;
		this.requestCallback = requestCallback;
	}

	public void sendRequest() throws RequestException {
		Method method = null;
		if(method!=null&&"POST".equals(this.method.toUpperCase()))
			method = RequestBuilder.POST;
		else {
			method = RequestBuilder.GET;
		}
	
		RequestBuilder builder = new RequestBuilder(method, (!url.contains("http://")?GWT
				.getHostPageBaseURL():null)+ URL.encode(url));
		
		builder.setCallback(requestCallback);
		builder.send();
			
		
	}
	
	
	 
	    
	
	
	
	
}
